package com.canbot.u05.activity.presidemode.newcode.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.PicturnEmotionBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClick;
import com.canbot.u05.socket.IpMsgConst;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NChoicePictureEmotionAdapter extends RecyclerView.Adapter<NChoicePictureEmotionAdapter.NChoiceDanceViewHolder> {
    private final Context mContext;

    private List<PicturnEmotionBean> mTaskList;
    private LayoutInflater layoutInflater;
    private IItemClick mIItemClick;
    private int mPrevCheckPos = -1;
    private int type;

    public NChoicePictureEmotionAdapter(int intentIntExtra, List<PicturnEmotionBean> taskList, Context context, IItemClick iItemClick) {
        this.mTaskList = taskList;
        this.type = intentIntExtra;
        if (mTaskList == null) {
            mTaskList = new ArrayList<>();
        }
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        this.mIItemClick = iItemClick;
    }

    @NonNull
    @Override
    public NChoiceDanceViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_choice_picture_emotion, null, false);
        return new NChoiceDanceViewHolder(view);
    }

    private static final String TAG = "NChoiceMusicAdapter";

    @Override
    public void onBindViewHolder(@NonNull NChoiceDanceViewHolder viewHolder, final int postion) {
        final PicturnEmotionBean picturnEmotionBean = mTaskList.get(postion);
        if (picturnEmotionBean != null) {
            viewHolder.ivSelect.setVisibility(picturnEmotionBean.isSelect() ? View.VISIBLE : View.INVISIBLE);
        }
        viewHolder.ivContent.setOnClickListener(v -> {
            if (mIItemClick != null) {
                mIItemClick.onClick(postion, v);
            }
        });
        if (type == NModuleType.PICTURE) {
            String fileName = picturnEmotionBean.getFileName();
            Glide.with(mContext).clear(viewHolder.ivContent);
            Glide.with(mContext)
                    .asDrawable()
                    .load(IpMsgConst.HTTP_URL_CHEST + fileName.replace("/uupic/", "/uupic_thumb/"))
                    .thumbnail(0.1f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                    .skipMemoryCache(false)
                    .placeholder(R.drawable.sales_banner01)//占位图
                    .error(R.drawable.ic_picture_loadfailed)
                    .override(100, 100)
                    .into(viewHolder.ivContent);
        } else {
            viewHolder.name.setVisibility(View.VISIBLE);
            String emotionType = picturnEmotionBean.getFileName();
            switch (emotionType) {
                case NModuleType.EMOTION_XI:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.xi);
                    viewHolder.name.setText(mContext.getString(R.string.xi));
                    break;
                case NModuleType.EMOTION_NU:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.nu);
                    viewHolder.name.setText(mContext.getString(R.string.nu));
                    break;
                case NModuleType.EMOTION_AI:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.ai);
                    viewHolder.name.setText(mContext.getString(R.string.ai));
                    break;
                case NModuleType.EMOTION_SI:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.si);
                    viewHolder.name.setText(mContext.getString(R.string.si));
                    break;
                case NModuleType.EMOTION_DAIJI:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.daiji);
                    viewHolder.name.setText(mContext.getString(R.string.daiji));
                    break;
                case NModuleType.EMOTION_JING:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.jing);
                    viewHolder.name.setText(mContext.getString(R.string.jing));
                    break;
                case NModuleType.EMOTION_KONG:
                    viewHolder.ivContent.setBackgroundResource(R.drawable.kong);
                    viewHolder.name.setText(mContext.getString(R.string.kong));
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }

    public class NChoiceDanceViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_content)
        ImageView ivContent;
        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.iv_name)
        TextView name;

        public NChoiceDanceViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
